// pages/cognize/cognize.js
const app=getApp();
const plugin = requirePlugin('WechatSI');
Page({

    /**
     * 页面的初始数据
     */
    data: {
    },

    createAudioSrc:function(cognize){
        plugin.textToSpeech({
            lang:'zh_CN',
            tts:true,
            content:cognize.Cname,
            success:ret=>{
                this.cAudioSrc=ret.filename
            },
            fail:ret=>{
                console.log('fail tts',ret)
            }
        }),
        plugin.textToSpeech({
            lang:'en_US',
            tts:true,
            content:cognize.Ename,
            success:ret=>{
                this.eAudioSrc=ret.filename
            },
            fail:ret=>{
                console.log('fail tts',ret)
            }
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    cognizes:null,
    currentIndex:0,
    cAudioCtx:null,
    eAudioCtx:null,
    cAudioSrc:null,
    eAudioSrc:null,
    user:null,
    onLoad: function (options) {
        let that=this;
        this.user=wx.getStorageSync('user');
        this.setData({userPhoto:this.user.head_Photo,userName:this.user.userName})
        let cognize_Classify=options.cognize_Classify;
        that.cAudioCtx=wx.createInnerAudioContext();
        that.eAudioCtx=wx.createInnerAudioContext();
        wx.request({
            url:`${app.globalData.url}/cognizes/${cognize_Classify}`,
            success:res=>{
                if(res.statusCode==200){
                    that.cognizes=res.data;
                    that.setData({
                        currentIndex:0,
                        cognize:that.cognizes[0],
                        amount:options.amount
                    });
                    that.createAudioSrc(that.data.cognize)
                }
            }
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {
        this.cAudioCtx.pause();
        this.eAudioCtx.pause();
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },

    towardsLeft:function(){
        this.setData({
            currentIndex:this.data.currentIndex-1,
            cognize:this.cognizes[this.data.currentIndex-1]})
        this.createAudioSrc(this.data.cognize)
    },

    towardsRight:function(){
        this.setData({
            currentIndex:this.data.currentIndex+1,
            cognize:this.cognizes[this.data.currentIndex+1]})
        this.createAudioSrc(this.data.cognize)
    },

    playChineseAudio:function(){
        this.cAudioCtx.src=this.cAudioSrc;
        this.cAudioCtx.play();
    },

    playEnglishAudio:function(){
        this.eAudioCtx.src=this.eAudioSrc;
        this.eAudioCtx.play();
    },
  
})